package models.System.Collection;

import controllers.pc.users.Recharges;
import models.Models.SystemUserCollection;
import models.Models.SystemUserFolders;
import models.SystemModels.MaterialInfo;
import models.SystemModels.ShopDetails;
import models.SystemModels.SystemUser;
import models.SystemModels.folders;
import models.interfaces.Systems;
import play.Logger;
import play.Play;
import play.libs.Files;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-06-24
 */
public class Collection{

    private static Systems systems = new models.System.Systems.Systems();

    /**
     * 建立用户收藏指定文件夹，用于储存文件
     * @param name
     * @return
     */
    public static String  Collection(String name){
        try{
            List<SystemUser> systemUsers = SystemUser.find("Id",name).fetch();
            if (systemUsers.isEmpty()){
                return null;
            }else{

                List<folders> list = folders.find("userId", systemUsers.get(0).Id.toString()).fetch();
                if (list.isEmpty()){
                    Long id= systemUsers.get(0).Id;

                    String file = String.format("%s\\Admin_Design\\public\\folder", Play.applicationPath.getParentFile().getAbsoluteFile().toString());
                    String userFileName =String.format("%s\\%s",file,id.toString());
                    String userCollectionFileName =String.format("%s\\%s\\%s",file,id.toString(),"Collection");
                    Logger.info(file);
                    String userFile =null;
                    File files = new File(file);
                    if(!(files.exists()||files.isFile())){
                        files.mkdir();
                    }

                    File userfiles = new File(String.format("%s",userFileName));
                    if (!userfiles.exists()||files.isFile()){
                        userfiles.mkdir();
                    }

                    File userCollectionFiles = new File(String.format("%s",userCollectionFileName));
                    if (!userCollectionFiles.exists()||userCollectionFiles.isFile()){
                        userfiles.mkdir();
                        userFile = String.format("%s\\%s\\%s\\","\\public\\folder",id.toString(),"Collection");
                    }
                    return userFile;
                }
                return null;
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }

    }

    /**
     * 检测用户文件信息，用于检测用户信息
     * @return
     */
    public static boolean UserCollection(){
        List<MaterialInfo> MaterialInfos = MaterialInfo.find("userId", Recharges.id()).fetch();
        if (MaterialInfos.isEmpty()){
            return false;
        }else{
            return true;
        }
    }
    /**
     * 首先建立用户文件夹，如果不存在直接建立，存在直接储存
     * @param system
     * @return
     */
    public static boolean UsersCollection(SystemUserCollection system){
        if (Collection(system.SystemUser).isEmpty()){
            if (UserCollection()){
                SystemUserFolders systemUser = new SystemUserFolders(system.ImgUrl,system.file);
                if (save(systemUser)){
                    MaterialInfo materialInfo = new MaterialInfo();
                    materialInfo.Dates = systems.dates(new Date());
                    materialInfo.folders = systems.Folders(system.ImgUrl);
                    materialInfo.ShopDetails = systems.ShopDetailsId(system.ImgUrl);
                    materialInfo.SystemUser = Recharges.id();
                    materialInfo.save();
                    return true;
                }
                else{
                    return false;
                }
            }else{
                return false;
            }
        }
        else{
            return true;
        }

    }

    /**
     *用于收藏图片信息
     * @param system
     * @return
     */
    public static boolean save(SystemUserFolders system) {
        try{
            String imgName=system.file.getName();
            String filePath = String.format("%s/%s",system.ImgUrl,imgName);
            File storeFile=new File(filePath);
            Files.copy(system.file, storeFile);
            return true;
        }catch (Exception ex){
            return false;
        }

    }

    /**
     * 用于增加浏览量
     * @param url
     * @return
     */
    public static boolean ShopDetailsBrowse(String url){
       try{
           List<ShopDetails> shopDetails = ShopDetails.find("picture",url).fetch();
           ShopDetails shopDetail =ShopDetails.findById( shopDetails.get(0).Id);
           shopDetail.browse = String.format("%s",(Integer.parseInt(shopDetail.browse)+1));
           shopDetail.save();
           return true;
       }catch (Exception ex){
           return false;
       }
    }

    /**
     * 用于增加收藏数量
     * @param url
     * @return
     */
    public static boolean ShopDetailsCollection(String url){
        try{
            List<ShopDetails> shopDetails = ShopDetails.find("picture",url).fetch();
            ShopDetails shopDetail =ShopDetails.findById( shopDetails.get(0).Id);
            shopDetail.Collection = String.format("%s",(Integer.parseInt(shopDetail.Collection)+1));
            shopDetail.save();
            return true;
        }catch (Exception ex){
            return false;
        }
    }
}
